package com.lfy.modules.common.config.thread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
 * 线程池配置
 *
 * @author <a href="https://www.lvfeiy.top">lvfeiyang</a>
 * @date 2024/4/2 14:14
 */
@Configuration
@Slf4j
public class ThreadPoolConfig  {

    public static  int CORE_SIZE = 4;
    public static  int MAX_SIZE = 8;
    public static  int QUEUE_SIZE = 200;

      {
         int nowComputerCoreNum = Runtime.getRuntime().availableProcessors();
         log.info("当前运行环境的CPU核数为：{}",nowComputerCoreNum);

     }

    /**
     * 核心重要任务线程池
     * @return
     */
    @Bean("mainThreadPool")
    public ThreadPoolExecutor mainThreadPool() {

        return new ThreadPoolExcecutorMdc(CORE_SIZE,MAX_SIZE,5, TimeUnit.MINUTES,new MyLinkedBlockingQueue<>(QUEUE_SIZE),
                new NameThreadFactory("main-pool-"),new ThreadPoolExecutor.CallerRunsPolicy());
    }
    @Bean("secondaryThreadPool")
    public ThreadPoolExecutor secondaryThreadPool() {
        return new ThreadPoolExcecutorMdc(1,2,1, TimeUnit.MINUTES,new MyLinkedBlockingQueue<>(100),
                new NameThreadFactory("secondary-pool-"),new ThreadPoolExecutor.DiscardPolicy());
    }

}
